package com.htbb.base.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.Pipeline;
import com.itextpdf.tool.xml.XMLWorker;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.parser.XMLParser;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline;
import com.itextpdf.tool.xml.pipeline.html.AbstractImageProvider;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;

/**
 * 生成pdf工具类
 * 
 * @author wangzhicheng
 *
 */
public class PDFUtil {

	public static final String HTMLPATH = PDFUtil.class.getResource("/").getPath().replaceAll("%20", " ")
			+ "PDFTemplate/pdf.html";
	public static final String BODYPATH = PDFUtil.class.getResource("/").getPath().replaceAll("%20", " ")
			+ "PDFTemplate/body.html";
	public static final String TAILPATH = PDFUtil.class.getResource("/").getPath().replaceAll("%20", " ")
			+ "PDFTemplate/tail.html";
	private static final String IMGPATH = PDFUtil.class.getResource("/").getPath().replaceAll("%20", " ")
			+ "PDFTemplate/img/";

	/**
	 * 模板字符串转成pdf文件到目标目录
	 * 
	 * @param source
	 */
	public static final void string2pdf(String source,String targetPath) {
		FontFactory.registerDirectories();
		Document document = new Document(PageSize.A4);
		PdfWriter writer;
		try {
			writer = PdfWriter.getInstance(document, new FileOutputStream(targetPath));
			document.open();
			
			HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
			htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

			htmlContext.setImageProvider(new AbstractImageProvider() {

				public String getImageRootPath() {

					 return IMGPATH;

				}

			});
			CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true);

			Pipeline<?> pipeline =

					new CssResolverPipeline(cssResolver,

							new HtmlPipeline(htmlContext,

									new PdfWriterPipeline(document, writer)));
			 //XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(source.getBytes()));

			XMLWorker worker = new XMLWorker(pipeline, true);

			XMLParser p = new XMLParser(worker);

			p.parse(new ByteArrayInputStream(source.getBytes()),Charset.forName("UTF-8"));

			document.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 从html模板中读取数据拼结成字符串
	 * 
	 * @return
	 */
	public static final String readHtmlTemplate(String path) {
		StringBuffer sb = new StringBuffer();
		String line = null;
		try {
			FileInputStream fin = new FileInputStream(path);
			InputStreamReader reader = new InputStreamReader(fin);
			BufferedReader br = new BufferedReader(reader);
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			br.close();
			reader.close();
			fin.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		/*String str = readHtmlTemplate();
		String pdfStr = str.replaceAll("#data#", "<tr><th>tgdgfdf</th><td>gdfgg8888</td></tr>");
		System.out.println(pdfStr);
		string2pdf(pdfStr,TARGETPATH);*/
	}


}
